
#include "lcd_HXR040.h"

void lcd_HXR040_reg_init(lcd_HXR040_t *t)
{
    
	t->bus->write_cmd(t->bus_handle, 0xFF);
	t->bus->write_data8(t->bus_handle, 0x77);
	t->bus->write_data8(t->bus_handle, 0x01);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x13);
	t->bus->write_cmd(t->bus_handle, 0xEF);
	t->bus->write_data8(t->bus_handle, 0x08);
	t->bus->write_cmd(t->bus_handle, 0xFF);
	t->bus->write_data8(t->bus_handle, 0x77);
	t->bus->write_data8(t->bus_handle, 0x01);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x10);
	t->bus->write_cmd(t->bus_handle, 0xC0);
	t->bus->write_data8(t->bus_handle, 0x3B);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_cmd(t->bus_handle, 0xC1);
	t->bus->write_data8(t->bus_handle, 0x0D);
	t->bus->write_data8(t->bus_handle, 0x02);
	t->bus->write_cmd(t->bus_handle, 0xC2);
	t->bus->write_data8(t->bus_handle, 0x21);
	t->bus->write_data8(t->bus_handle, 0x08);
	t->bus->write_cmd(t->bus_handle, 0xCD);
	t->bus->write_data8(t->bus_handle, 0x08);//18-bit/pixel: MDT=0:D[21:16]=R,D[13:8]=G,D[5:0]=B(CDH=00) ;
	t->bus->write_cmd(t->bus_handle, 0xB0);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x11);
	t->bus->write_data8(t->bus_handle, 0x18);
	t->bus->write_data8(t->bus_handle, 0x0E);
	t->bus->write_data8(t->bus_handle, 0x11);
	t->bus->write_data8(t->bus_handle, 0x06);
	t->bus->write_data8(t->bus_handle, 0x07);
	t->bus->write_data8(t->bus_handle, 0x08);
	t->bus->write_data8(t->bus_handle, 0x07);
	t->bus->write_data8(t->bus_handle, 0x22);
	t->bus->write_data8(t->bus_handle, 0x04);
	t->bus->write_data8(t->bus_handle, 0x12);
	t->bus->write_data8(t->bus_handle, 0x0F);
	t->bus->write_data8(t->bus_handle, 0xAA);
	t->bus->write_data8(t->bus_handle, 0x31);
	t->bus->write_data8(t->bus_handle, 0x18);
	t->bus->write_cmd(t->bus_handle, 0xB1);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x11);
	t->bus->write_data8(t->bus_handle, 0x19);
	t->bus->write_data8(t->bus_handle, 0x0E);
	t->bus->write_data8(t->bus_handle, 0x12);
	t->bus->write_data8(t->bus_handle, 0x07);
	t->bus->write_data8(t->bus_handle, 0x08);
	t->bus->write_data8(t->bus_handle, 0x08);
	t->bus->write_data8(t->bus_handle, 0x08);
	t->bus->write_data8(t->bus_handle, 0x22);
	t->bus->write_data8(t->bus_handle, 0x04);
	t->bus->write_data8(t->bus_handle, 0x11);
	t->bus->write_data8(t->bus_handle, 0x11);
	t->bus->write_data8(t->bus_handle, 0xA9);
	t->bus->write_data8(t->bus_handle, 0x32);
	t->bus->write_data8(t->bus_handle, 0x18);
	t->bus->write_cmd(t->bus_handle, 0xFF);
	t->bus->write_data8(t->bus_handle, 0x77);
	t->bus->write_data8(t->bus_handle, 0x01);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x11);
	t->bus->write_cmd(t->bus_handle, 0xB0);
	t->bus->write_data8(t->bus_handle, 0x60);
	t->bus->write_cmd(t->bus_handle, 0xB1);
	t->bus->write_data8(t->bus_handle, 0x30);
	t->bus->write_cmd(t->bus_handle, 0xB2);
	t->bus->write_data8(t->bus_handle, 0x87);
	t->bus->write_cmd(t->bus_handle, 0xB3);
	t->bus->write_data8(t->bus_handle, 0x80);
	t->bus->write_cmd(t->bus_handle, 0xB5);
	t->bus->write_data8(t->bus_handle, 0x49);
	t->bus->write_cmd(t->bus_handle, 0xB7);
	t->bus->write_data8(t->bus_handle, 0x85);
	t->bus->write_cmd(t->bus_handle, 0xB8);
	t->bus->write_data8(t->bus_handle, 0x21);
	t->bus->write_cmd(t->bus_handle, 0xC1);
	t->bus->write_data8(t->bus_handle, 0x78);
	t->bus->write_cmd(t->bus_handle, 0xC2);
	t->bus->write_data8(t->bus_handle, 0x78);
	hal_delay_ms(20);
	t->bus->write_cmd(t->bus_handle, 0xE0);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x1B);
	t->bus->write_data8(t->bus_handle, 0x02);
	t->bus->write_cmd(t->bus_handle, 0xE1);
	t->bus->write_data8(t->bus_handle, 0x08);
	t->bus->write_data8(t->bus_handle, 0xA0);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x07);
	t->bus->write_data8(t->bus_handle, 0xA0);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x44);
	t->bus->write_data8(t->bus_handle, 0x44);
	t->bus->write_cmd(t->bus_handle, 0xE2);
	t->bus->write_data8(t->bus_handle, 0x11);
	t->bus->write_data8(t->bus_handle, 0x11);
	t->bus->write_data8(t->bus_handle, 0x44);
	t->bus->write_data8(t->bus_handle, 0x44);
	t->bus->write_data8(t->bus_handle, 0xED);
	t->bus->write_data8(t->bus_handle, 0xA0);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0xEC);
	t->bus->write_data8(t->bus_handle, 0xA0);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_cmd(t->bus_handle, 0xE3);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x11);
	t->bus->write_data8(t->bus_handle, 0x11);
	t->bus->write_cmd(t->bus_handle, 0xE4);
	t->bus->write_data8(t->bus_handle, 0x44);
	t->bus->write_data8(t->bus_handle, 0x44);
	t->bus->write_cmd(t->bus_handle, 0xE5);
	t->bus->write_data8(t->bus_handle, 0x0A);
	t->bus->write_data8(t->bus_handle, 0xE9);
	t->bus->write_data8(t->bus_handle, 0xD8);
	t->bus->write_data8(t->bus_handle, 0xA0);
	t->bus->write_data8(t->bus_handle, 0x0C);
	t->bus->write_data8(t->bus_handle, 0xEB);
	t->bus->write_data8(t->bus_handle, 0xD8);
	t->bus->write_data8(t->bus_handle, 0xA0);
	t->bus->write_data8(t->bus_handle, 0x0E);
	t->bus->write_data8(t->bus_handle, 0xED);
	t->bus->write_data8(t->bus_handle, 0xD8);
	t->bus->write_data8(t->bus_handle, 0xA0);
	t->bus->write_data8(t->bus_handle, 0x10);
	t->bus->write_data8(t->bus_handle, 0xEF);
	t->bus->write_data8(t->bus_handle, 0xD8);
	t->bus->write_data8(t->bus_handle, 0xA0);
	t->bus->write_cmd(t->bus_handle, 0xE6);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x11);
	t->bus->write_data8(t->bus_handle, 0x11);
	t->bus->write_cmd(t->bus_handle, 0xE7);
	t->bus->write_data8(t->bus_handle, 0x44);
	t->bus->write_data8(t->bus_handle, 0x44);
	t->bus->write_cmd(t->bus_handle, 0xE8);
	t->bus->write_data8(t->bus_handle, 0x09);
	t->bus->write_data8(t->bus_handle, 0xE8);
	t->bus->write_data8(t->bus_handle, 0xD8);
	t->bus->write_data8(t->bus_handle, 0xA0);
	t->bus->write_data8(t->bus_handle, 0x0B);
	t->bus->write_data8(t->bus_handle, 0xEA);
	t->bus->write_data8(t->bus_handle, 0xD8);
	t->bus->write_data8(t->bus_handle, 0xA0);
	t->bus->write_data8(t->bus_handle, 0x0D);
	t->bus->write_data8(t->bus_handle, 0xEC);
	t->bus->write_data8(t->bus_handle, 0xD8);
	t->bus->write_data8(t->bus_handle, 0xA0);
	t->bus->write_data8(t->bus_handle, 0x0F);
	t->bus->write_data8(t->bus_handle, 0xEE);
	t->bus->write_data8(t->bus_handle, 0xD8);
	t->bus->write_data8(t->bus_handle, 0xA0);
	t->bus->write_cmd(t->bus_handle, 0xEB);
	t->bus->write_data8(t->bus_handle, 0x02);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0xE4);
	t->bus->write_data8(t->bus_handle, 0xE4);
	t->bus->write_data8(t->bus_handle, 0x88);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x40);
	t->bus->write_cmd(t->bus_handle, 0xEC);
	t->bus->write_data8(t->bus_handle, 0x3C);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_cmd(t->bus_handle, 0xED);
	t->bus->write_data8(t->bus_handle, 0xAB);
	t->bus->write_data8(t->bus_handle, 0x89);
	t->bus->write_data8(t->bus_handle, 0x76);
	t->bus->write_data8(t->bus_handle, 0x54);
	t->bus->write_data8(t->bus_handle, 0x02);
	t->bus->write_data8(t->bus_handle, 0xFF);
	t->bus->write_data8(t->bus_handle, 0xFF);
	t->bus->write_data8(t->bus_handle, 0xFF);
	t->bus->write_data8(t->bus_handle, 0xFF);
	t->bus->write_data8(t->bus_handle, 0xFF);
	t->bus->write_data8(t->bus_handle, 0xFF);
	t->bus->write_data8(t->bus_handle, 0x20);
	t->bus->write_data8(t->bus_handle, 0x45);
	t->bus->write_data8(t->bus_handle, 0x67);
	t->bus->write_data8(t->bus_handle, 0x98);
	t->bus->write_data8(t->bus_handle, 0xBA);
	t->bus->write_cmd(t->bus_handle, 0xEF);
	t->bus->write_data8(t->bus_handle, 0x10);
	t->bus->write_data8(t->bus_handle, 0x0D);
	t->bus->write_data8(t->bus_handle, 0x04);
	t->bus->write_data8(t->bus_handle, 0x08);
	t->bus->write_data8(t->bus_handle, 0x3F);
	t->bus->write_data8(t->bus_handle, 0x1F);
	t->bus->write_cmd(t->bus_handle, 0xFF);
	t->bus->write_data8(t->bus_handle, 0x77);
	t->bus->write_data8(t->bus_handle, 0x01);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_cmd(t->bus_handle, 0x3A);
	t->bus->write_data8(t->bus_handle, 0x66);//55/50=16bit(RGB565);66=18bit(RGB666);77?????3AH?=24bit(RGB888)

	t->bus->write_cmd(t->bus_handle, 0xFF);
	t->bus->write_data8(t->bus_handle, 0x77);
	t->bus->write_data8(t->bus_handle, 0x01);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x13);
	t->bus->write_cmd(t->bus_handle, 0xE8);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x0E);
	t->bus->write_cmd(t->bus_handle, 0xFF);
	t->bus->write_data8(t->bus_handle, 0x77);
	t->bus->write_data8(t->bus_handle, 0x01);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_cmd(t->bus_handle, 0x11);
	hal_delay_ms(120);
	t->bus->write_cmd(t->bus_handle, 0xFF);
	t->bus->write_data8(t->bus_handle, 0x77);
	t->bus->write_data8(t->bus_handle, 0x01);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x13);
	t->bus->write_cmd(t->bus_handle, 0xE8);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x0C);
	hal_delay_ms(10);
	t->bus->write_cmd(t->bus_handle, 0xE8);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_cmd(t->bus_handle, 0xFF);
	t->bus->write_data8(t->bus_handle, 0x77);
	t->bus->write_data8(t->bus_handle, 0x01);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);

	t->bus->write_cmd(t->bus_handle, 0x35);
	t->bus->write_data8(t->bus_handle, 0x00);//TE ON

    t->bus->write_cmd(t->bus_handle, 0x36); //D3:BGR
	t->bus->write_data8(t->bus_handle, 0x00);//FW:00     BW:10

	t->bus->write_cmd(t->bus_handle, 0x21);


	///////////////BIST////////////////
/*	t->bus->write_cmd(t->bus_handle, 0xFF); 
	t->bus->write_data8(t->bus_handle, 0x77);
	t->bus->write_data8(t->bus_handle, 0x01);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x00);
	t->bus->write_data8(t->bus_handle, 0x12);
	t->bus->write_cmd(t->bus_handle, 0xD1); 
	t->bus->write_data8(t->bus_handle, 0x81);
	t->bus->write_cmd(t->bus_handle, 0xD2); 
	t->bus->write_data8(t->bus_handle, 0x06);	
*/
	t->bus->write_cmd(t->bus_handle, 0x11);
	hal_delay_ms(120);

	t->bus->write_cmd(t->bus_handle, 0x29);
	hal_delay_ms(120);
	  
}

void lcd_HXR040_cmd(lcd_HXR040_t *t, uint8_t cmd)
{    
    t->bus->write_cmd(t->bus_handle, cmd);
}

void lcd_HXR040_init(lcd_HXR040_t *t)
{
    t->base.x_size = 480;
    t->base.y_size = 480;
    t->base.rst_time1 = 50;
    t->base.rst_time2 = 100;
    
    t->bus->reset(t->bus_handle, t->base.rst_time1, t->base.rst_time2);
    lcd_HXR040_reg_init(t);
    t->base.cmd_group.dis_on = 0x29;
    t->base.cmd_group.dis_off = 0x28;
    
    t->draw_complete = xSemaphoreCreateBinary();
    
    lcd_HXR040_clear(t, 0x0000);
}

void lcd_HXR040_clear(lcd_HXR040_t *t, uint16_t color)
{

}

void lcd_HXR040_draw_rect(lcd_HXR040_t *t, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, const uint8_t *color_p)
{

}

void lcd_HXR040_draw_end(lcd_HXR040_t *t)
{

}

void lcd_HXR040_draw_complete_wait(lcd_HXR040_t *t)
{
}
